﻿using Engine;
using GameEntitySystem;
using TemplatesDatabase;
namespace Mekiasm
{
    public class ComponentItemEnergyMachine : ComponentEnergyMachine, Interface.IMekItemMachine
    {
        public Vector3 StartPosition { get; set; }
        public Vector3 NowPosition { get; set; }
        public Vector3 EndPosition { get; set; }
        public Vector3 Velocity { get; set; }
        public int MoveFromFace { get; set; }
        public int? Value { get; set; }
        public bool MoveEndFlag { get; set; }
        public bool IsStartAnimate { get; set; }
        public float SpeedControl { get; set; } = 0.01f;


        public override void Save(ValuesDictionary valuesDictionary, EntityToIdMap entityToIdMap)
        {
            base.Save(valuesDictionary, entityToIdMap);
            SaveItemData(valuesDictionary);
        }
        public override void Load(ValuesDictionary valuesDictionary, IdToEntityMap idToEntityMap)
        {
            base.Load(valuesDictionary, idToEntityMap);
            LoadItemData(valuesDictionary);
        }

        public void LoadItemData(ValuesDictionary valuesDictionary)
        {
            ValuesDictionary pairs = valuesDictionary.GetValue<ValuesDictionary>("ItemData");
            StartPosition = pairs.GetValue("StartPosition", Vector3.Zero);
            if (pairs.GetValue("HasValue", false)) Value = pairs.GetValue<int>("Value", 0);
            NowPosition = pairs.GetValue("NowPosition", Vector3.Zero);
            EndPosition = pairs.GetValue("EndPosition", Vector3.Zero);
            Velocity = pairs.GetValue("Velocity", Vector3.Zero);
            MoveFromFace = pairs.GetValue("MoveFromFace", 0);
            MoveEndFlag = pairs.GetValue("MoveEndFlag", false);
            switch (InputPowerLimit)
            {
                case 32: SpeedControl = 0.01f; break;
                case 128: SpeedControl = 0.04f; break;
                case 512: SpeedControl = 0.16f; break;
                case 2048: SpeedControl = 0.64f; break;
                default: SpeedControl = 0.01f; break;
            }
            IsStartAnimate = pairs.GetValue<bool>("IsStartAnimate", false);
        }

        public void SaveItemData(ValuesDictionary valuesDictionary)
        {
            ValuesDictionary valuePairs = new ValuesDictionary();
            if (Value.HasValue)
            {
                valuePairs.SetValue("Value", Value.Value);
            }
            valuePairs.SetValue("HasValue", Value.HasValue);
            valuePairs.SetValue("StartPosition", StartPosition);
            valuePairs.SetValue("NowPosition", NowPosition);
            valuePairs.SetValue("EndPosition", EndPosition);
            valuePairs.SetValue("Velocity", Velocity);
            valuePairs.SetValue("MoveFromFace", MoveFromFace);
            valuePairs.SetValue("MoveEndFlag", MoveEndFlag);
            valuePairs.SetValue("IsStartAnimate", IsStartAnimate);
            valuesDictionary.SetValue("ItemData", valuePairs);
        }
    }
}
